home *** CD-ROM | disk | FTP | other *** search
- #include <WindowMgr.h>
- #include <MenuMgr.h>
- #include <EventMgr.h>
- #include "Skel defines.h"
- #include "Skel globals.h"
-
- #include <EventMgr.h>
-
- /*
- the main loop that handles events
- ############################ MainEventLoop ##########################
-
- Brace yourself: here's where the action is. Most Mac programs just
- wait for events (as do we all), and then process them. There are
- two sorts of events: those directly initiated by the user, like key
- presses and mouse-downs, and those consequent events posted by the
- Event Manager, like update and activate events. The latter MUST be
- handled correctly and carefully. In particular, it's important for
- all events to make sure that the event occurred in or for the window
- you expect -- it's possible to get events which are not for one of
- our windows, despite GetNextEvent's return value. Similarly,
- be sure to check that the window it occured in is the active one,
- if it matters.
-
- A common mistake in handling update and activate events is in finding
- out which window they are for. It is "(WindowPtr)myevent.message "
- that gives this information, NOT "whichwindow" (the WindowPtr returned
- by FindWindow). The latter pointer merely tells you what window
- the mouse was in at the time the event was posted -- completely
- irrelevant for Update and Activate events. Think it through carefully.
-
- */
-
- maineventloop () {
-
- /* body of MainEventLoop */
-
- FlushEvents (everyEvent, 0);/* discard leftover events */
-
- /* get next event, and Handle it appropriately, until user QUITs */
-
- userdone = 0;
- do {
- SystemTask (); /* Handle desk accessories */
- if (GetNextEvent (everyEvent, &myevent)) {
- /* get event; if for us... */
- switch (myevent.what) {/* Handle each kind of event */
- case mouseDown: /* find out what window the mouse went
- down in, and where in it */
- windowcode = FindWindow (myevent.where, &whichwindow);
- switch (windowcode) {
- /* Handle mouse-down for each place */
- case inSysWindow:
- /* Handle the desk accessories */
- SystemClick (&myevent, whichwindow);
- break;/* inSysWindow */
- case inMenuBar: /* Handle the command */
- userdone = docommand (MenuSelect (myevent.where));
- break;/* inMenuBar */
- case inDrag: /* drag the window */
- DragWindow (whichwindow, myevent.where, &dragrect);
- break;
- case inContent:
- /* includes inGrow if window inactive.
- Activate window */
- if (whichwindow == mywindow)
- /* make sure it's for mine */
- if (whichwindow != FrontWindow ())
- SelectWindow (whichwindow);
- /* make it active */
- break;
- case inGrow:
- /* window is already active; change its
- size */
- if (whichwindow == mywindow)
- /* moke sure it's for mine */
- resize (mywindow, myevent.where);
- break;/* inGrow */
- case inGoAway:
- /* we don't have a GoAway Region */
- /* break; I got a compiler error here with the
- break left in. Good C programming Style would
- have a break even after the last case - WHJ
- 3/11/85 */
- break; /* put it in for LSC */
- }
- break; /* switch */
-
- case keyDown:
- case autoKey: /* if command key, pass the char to
- MenuKey */
- if ((myevent.modifiers & cmdKey) != 0)
- userdone = docommand (MenuKey ((char) (myevent.message & charcodemask)));
- case updateEvt: /* if it's for our window, update it */
- if ((WindowPtr) (myevent.message) == mywindow)
- updatewindow (mywindow);
- /* redraw the window contents */
- break;
- case activateEvt:
- /* if for our window, set port as nec. */
- if ((WindowPtr) (myevent.message) == mywindow) {
- /* my window */
- DrawGrowIcon (mywindow);
- /* redraw grow icon to reflect new state
- */
- if (myevent.modifiers & 1)
- /* odd means an activate event */
- SetPort (mywindow);
- /* activate evt: work in our own port */
- else
- SetPort (screenport);
- /* deactivate evt: our port is gone; keep
- port from dangling */
- } break;
- }
- }
-
- } while (userdone == 0);
- }
-